{template 'header'}
<script type='text/javascript' src='../addons/we7_wmall/resource/app/js/iscroll-probe.js' charset='utf-8'></script>
<div class="page order-confirm">
	<header class="bar bar-nav">
		<a class="icon fa fa-arrow-left pull-left back" href="javascript:;"></a>
		<h1 class="title">提交订单</h1>
	</header>
	<nav class="bar bar-tab no-gutter order-bar">
		<div class="left">
			<span class="pull-left">
				已优惠
				<span class="activity-price">￥{$activityed['total']}</span>
			</span>
			<span class="pull-right">
				还需付
				<span class="sum"><span class="wait-price">￥{$waitprice}</span></span>
			</span>
		</div>
		<div class="right text-center bg-danger" id="order-submit">确认下单</div>
	</nav>
	{if !empty($activityed['list']['delivery'])}
		<nav class="bar bar-tab info-bar">
			<img src="{MODULE_URL}resource/app/img/vip_effective.png" alt="">
			尊贵的会员, 已为您节省{$activityed['list']['delivery']['value']}元
		</nav>
	{/if}
	<div class="content">
		<form method="post" id="order-form" action="{php echo $this->createMobileUrl('order', array('sid' => $sid, 'op' => 'submit'));}">
			<input type="hidden" name="sid" value="{$sid}">
			<input type="hidden" name="address_id" id="address_id" value="{$address_id}">
			<input type="hidden" name="record_id" id="record_id" value="{$recordid}">
			<input type="hidden" name="note" id="order-note" value="">
			<input type="hidden" name="delivery_time" id="delivery-time" value="{$predict_time}">
			<input type="hidden" name="delivery_day" id="delivery-day" value="{$predict_day}">
			{if $store['delivery_type'] == 2}
				<input type="radio" name="order_type" class="order_type hide" value="2" checked>
				<div class="content-block-title">选择配送方式 <span class="color-danger">（仅支持到店自提）</span></div>
			{/if}
			{if $store['delivery_type'] == 1}
				<input type="radio" name="order_type" class="order_type hide" value="1" checked>
			{/if}
			{if $store['delivery_type'] == 3}
				<div class="content-block-title">选择配送方式</div>
				<div class="list-block media-list pay-method">
					<ul>
						<li>
							<label class="label-checkbox item-content">
								<div class="item-inner">
									<div class="item-title">商家配送</div>
								</div>
								<input type="radio" name="order_type" class="order_type" value="1" checked id="order-type-1">
								<div class="item-media"><i class="icon icon-form-checkbox"></i></div>
							</label>
						</li>
						<li>
							<label class="label-checkbox item-content">
								<div class="item-inner">
									<div class="item-title">到店自提</div>
								</div>
								<input type="radio" name="order_type" class="order_type" value="2" id="order-type-2">
								<div class="item-media"><i class="icon icon-form-checkbox"></i></div>
							</label>
						</li>
					</ul>
				</div>
			{/if}
			{if $store['delivery_type'] == 2 || $store['delivery_type'] == 3}
			<div class="list-block {if $store['delivery_type'] == 3}hide{/if}" id="delivery-time-2">
				<ul>
					<li>
						<a href="javascript:;" class="item-link item-content delivery-time">
							<div class="item-inner">
								<div class="item-title">自提时间</div>
								<div class="item-after"><span class="color-black delivery-time-show">下单后直接去自提</span><span class="color-orange hide">(大约12:00到)</span></div>
							</div>
						</a>
					</li>
					<li>
						<div class="item-content">
							<div class="item-inner">
								<div class="item-title label">下单人</div>
								<div class="item-input">
									<input type="text" name="username" placeholder="您的姓名" value="{$_W['member']['realname']}">
								</div>
							</div>
						</div>
					</li>
					<li>
						<div class="item-content">
							<div class="item-inner">
								<div class="item-title label">手机号</div>
								<div class="item-input">
									<input type="text" name="mobile" placeholder="手机号" value="{$_W['member']['mobile']}">
								</div>
							</div>
						</div>
					</li>
				</ul>
			</div>
			{/if}
			{if $store['delivery_type'] == 1 || $store['delivery_type'] == 3}
			<div class="list-block address" id="address-container">
				<ul>
					<li>
						<a class="item-link item-content external" href="{php echo $this->createMobileUrl('address', array('sid' => $sid, 'r' => 1, 'recordid' => $_GPC['recordid']));}">
							<div class="item-inner">
								{if !empty($address)}
									<div class="item-text">
										<div><span class="name">{$address['realname']}</span><span class="tel">{$address['mobile']}</span></div>
										<div>地址:{$address['address']}</div>
									</div>
								{else}
									<div class="item-title">请选择送达地址</div>
								{/if}
							</div>
						</a>
						<div class="top-line"></div>
					</li>
					<li id="delivery-time-1">
						<a href="#" class="item-link item-content delivery-time">
							<div class="item-inner">
								<div class="item-title">请选择送达时间</div>
								<div class="item-after"><span class="color-black delivery-time-show">{$text_time}</span></div>
							</div>
						</a>
					</li>
				</ul>
			</div>
			{/if}
			<div class="content-block-title">选择支付方式 <span class="color-warning hide">（在线支付立减5元）</span></div>
			<div class="list-block media-list pay-method">
				<ul>
					{loop $store['payment'] $row}
						<li>
							<label class="label-checkbox item-content">
								<div class="item-inner">
									<div class="item-title">{$pay_types[$row]['text']}</div>
								</div>
								<input type="radio" name="pay_type" class="pay_type" value="{$row}" {if $row == 'wechat'}checked{/if}>
								<div class="item-media"><i class="icon icon-form-checkbox"></i></div>
							</label>
						</li>
					{/loop}
				</ul>
			</div>
			<div class="list-block coupon-info">
				<ul>
					<li>
						<a href="javascript:;" class="item-link item-content">
							<div class="item-inner">
								<div class="item-title">代金券</div>
								<div class="item-after color-danger {if !empty($coupons)}open-popup{/if}" data-popup=".popup-coupon">
								{$coupon_text}
								</div>
							</div>
						</a>
					</li>
					{if !empty($conpon)}
						<li class="help-block">
							{if $conpon['use_limit'] == 1}
							<div class="color-danger">本券可与其他优惠叠加使用</div>
							{else}
							<div class="color-gray">本券不可与其他优惠叠加使用</div>
							{/if}
						</li>
					{/if}
				</ul>
			</div>
			<div class="list-block">
				<ul>
					<li>
						<a class="item-link item-content">
							<div class="item-inner">
								<div class="item-title">备注</div>
								<div class="item-after order-note" id="order-note-show">(选填)可填入特殊要求</div>
							</div>
						</a>
					</li>
					{if $store['invoice_status']}
					<li>
						<div class="item-content invoice">
							<div class="item-inner">
								<div class="item-title">需要发票</div>
								<div class="item-after">
									<label class="label-switch invoice-status">
										<input type="checkbox" value="1">
										<div class="checkbox"></div>
									</label>
								</div>
							</div>
						</div>
					</li>
					{/if}
					<li class="hide invoice-value">
						<div class="item-content">
							<input type="text" id="invoice" placeholder="输入个人或者公司抬头"/>
						</div>
					</li>
				</ul>
			</div>
			<div class="list-block detail-info">
				<ul>
					<li>
						<div class="item-content">
							<div class="item-inner">
								<div class="item-title">{$store['title']}</div>
								<div class="item-after">本单由 <span class="color-black">{if $account['delivery_type'] == 1}商家{else}平台{/if}</span> 配送</div>
							</div>
						</div>
					</li>
					<li class="order-list">
						<div class="inner-con">
							{loop $cart['data'] $val}
								{loop $val $val1}
									<div class="row no-gutter">
										<div class="col-60">{$val1['title']}</div>
										<div class="col-20 text-right color-muted">×{$val1['num']}</div>
										<div class="col-20 text-right color-black">￥{$val1['price']}</div>
									</div>
								{/loop}
							{/loop}
						</div>
					</li>
					<li class="order-list">
						<div class="inner-con">
							<div class="row no-gutter">
								<div class="col-80">包装费</div>
								<div class="col-20 text-right color-black">￥{$store['pack_price']}</div>
							</div>
							<div class="row no-gutter">
								<div class="col-80">配送费</div>
								<div class="col-20 text-right color-black">￥<span id="delivery-price">{$delivery_price}</span></div>
							</div>
						</div>
					</li>
					{if !empty($activityed['list'])}
						<li class="order-list activity-list">
							<div class="inner-con">
								{loop $activityed['list'] $key $row}
									<div class="row no-gutter activity-{$key}">
										<div class="col-50 icon-before"><img src="{MODULE_URL}resource/app/img/{$row['icon']}">{$row['name']}</div>
										<div class="col-50 text-right color-black">{$row['text']}</div>
									</div>
								{/loop}
							</div>
						</li>
					{/if}
					<li class="order-list">
						<div class="inner-con">
							<div class="row no-gutter">
								<div class="col-60 color-muted">订单 <span class="color-black total-price">￥{php echo $cart['price'] + $store['pack_price'] + $delivery_price}</span> - 优惠 <span class="color-black activity-price">￥{$activityed['total']}</span></div>
								<div class="col-20 text-right color-muted">总计</div>
								<div class="col-20 text-right color-black"><span class="wait-price">￥{$waitprice}</span></div>
							</div>
						</div>
					</li>
					{if $info['member_level']!=0}
					<li class="order-list">
						<div class="inner-con">
							<div class="row no-gutter">
								<div class="col-80">会员优惠折扣</div>
								<div class="col-20 text-right color-black">{$info['discount']}折</div>
							</div>
						</div>
						<input type="hidden" id="discount" value="{$info['discount']}" />
					</li>
					{/if}
				</ul>
			</div>
		</form>
	</div>
</div>
<div class="modal modal-no-buttons delivery-time-modal not-remove">
	<div class="modal-inner">
		<div class="modal-title">
			<div>请选择送达时间</div>
		</div>
		<div class="modal-text">
			<div class="category-container">
				<div class="parent-category" id="delivery-time-parent">
					<ul>
						{loop $delivery_time['days'] $i $day}
							<li {if !$i}class="active"{/if} data-value="{$day}"><a href="javascript:;">{$day}</a></li>
						{/loop}
					</ul>
				</div>
				<div class="children-category" id="delivery-time-children">
					<div class="children-category-wrapper">
						<ul id="category1">
							{if $time_flag == 1}
								<li data-value="尽快送达" class="time-flag"><a href="javascript:;">尽快送达</a></li>
							{/if}
							{loop $delivery_time['times'] $i $time}
								<li data-value="{$time['start']} ~ {$time['end']}" class="{if $time['timestamp'] <= TIMESTAMP && !$delivery_time['reserve']}hide init-hide{/if}">
									<a href="javascript:;">{$time['start']} ~ {$time['end']}</a>
								</li>
							{/loop}
						</ul>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="popup popup-remark" id="popup-remark">
	<div class="content-block">
		<div class="popup-header row no-gutter">
			<div class="col-25"><a href=""><span class="fa fa-arrow-left"></span></a></div>
			<div class="col-50 text-center">填写备注</div>
			<div class="col-25 text-right"><a href="javascript:;" class="sure close-popup" id="note-submit">确定</a></div>
		</div>
		<div class="popup-body">
			<textarea name="" id="note-textarea" placeholder="给商家留言,可输入对商家的要求"></textarea>
			<div class="specs-select">
				<span class="spec-item">请提供餐具</span>
				<span class="spec-item">不吃辣</span>
				<span class="spec-item">微辣</span>
				<span class="spec-item">米饭多点</span>
				<span class="spec-item">没零钱</span>
			</div>
		</div>
	</div>
</div>
<div class="popup popup-coupon" id="popup-coupon">
	<div class="page coupon coupon-select">
		<header class="bar bar-nav common-bar-nav">
			<h1 class="title">选择代金券</h1>
		</header>
		<div class="content">
			<div class="content-padded">
				<a href="{php echo $this->createMobileUrl('submit', array('sid' => $sid, 'op' => 'index', 'address_id' => $_GPC['address_id']));}" class="button button-light button-big">不使用代金券</a>
			</div>
			<div class="coupon-list content-padded">
				{loop $coupons $coupon}
				<div class="coupon-list-item" data-recordid="{$coupon['id']}">
					<div class="coupon-panel">
						<label class="label-checkbox item-content">
							<input type="radio" name="recordid" value="{$coupon['id']}" class="coupon-radio">
							<div class="item-media">
								<i class="icon icon-form-checkbox"></i>
							</div>
							<div class="item-inner">
								<div class="row no-gutter">
									<div class="col-40 text-center">
										<div class="price"><span>￥</span>{$coupon['discount']}</div>
										<div class="condition">满{$coupon['condition']}元可用</div>
									</div>
									<div class="col-60">
										<div class="store-title">{$coupon['title']}</div>
										<div class="date">有效期至{php echo date('Y-m-d', $coupon['endtime'])}</div>
									</div>
								</div>
							</div>
						</label>
						<span class="scan-rules">代金券使用规则<span class="fa fa-arrow-down"></span></span>
					</div>
					<ol class="coupon-rules hide">
						<li>
							{if $coupon['use_limit'] == 1}
							可与其他优惠同享
							{else}
							不与其他优惠同享
							{/if}
						</li>
						<li>仅在线支付可用</li>
					</ol>
				</div>
				{/loop}
			</div>
		</div>
	</div>
</div>
<script>
$(function(){
	$(document).on('click', '.scan-rules', function(){
		var $parent = $(this).parents('.coupon-list-item');
		$parent.find('.coupon-rules').toggleClass('hide');
	});

	$(document).on('click', '#popup-coupon .coupon-radio', function(e){
		var recordid = $(this).parents('.coupon-list-item').data('recordid');
		$.closeModal('.popup-coupon');
		$.showIndicator();
		setTimeout(function(){
			location.href = "{php echo $this->createMobileUrl('submit', array('sid' => $sid, 'op' => 'index', 'address_id' => $_GPC['address_id']));}&recordid=" + recordid;
		}, 100);
		return;
	});

	$(document).on('click', '#order-submit', function(){
		var $this = $(this);
		if($(this).hasClass('bg-grey')) {
			return false;
		}
		$(this).removeClass('bg-danger').addClass('bg-grey');
		var order_type = $(':radio[name="order_type"]:checked').val();
		var username = '';
		var mobile = '';
		if(order_type == 1) {
			if(!$('#address_id').val()) {
				$(this).addClass('bg-danger').removeClass('bg-grey');
				$.toast('请选择收货地址');
				return false;
			}
		} else if(order_type == 2) {
			var username = $.trim($(':text[name="username"]').val());
			if(!username) {
				$(this).addClass('bg-danger').removeClass('bg-grey');
				$.toast('请填写下单人');
				return false;
			}
			var mobile = $.trim($(':text[name="mobile"]').val());
			var reg = /^1[34578][0-9]{9}$/;
			if(!reg.test(mobile)) {
				$(this).addClass('bg-danger').removeClass('bg-grey');
				$.toast("手机号格式错误");
				return false;
			}
		}

		if(!$('.pay_type:checked').val()) {
			$(this).addClass('bg-danger').removeClass('bg-grey');
			$.toast('请选择支付方式');
			return false;
		}
		var params = {
			delivery_day: $('#delivery-day').val(),
			delivery_time: $('#delivery-time').val(),
			address_id: $('#address_id').val(),
			record_id: $('#record_id').val(),
			note: $('#order-note').val(),
			pay_type: $('.pay_type:checked').val(),
			invoice: $('#invoice').val(),
			order_type: order_type,
			username: username,
			mobile: mobile
		};
		$.post("{php echo $this->createMobileUrl('submit', array('sid' => $sid, 'op' => 'submit'));}", params, function(data){
			var result = $.parseJSON(data);
			if(result.message.errno != 0) {
				$(this).addClass('bg-danger').removeClass('bg-grey');
				$.toast(result.message.message);
			} else {
				$.toast('下单成功');
				location.href = "{php echo $this->createMobileUrl('pay', array('sid' => $sid, 'order_type' => 'order'));}&id=" + result.message.message;
			}
			return false;
		});
	});

	var delivery_price = "{$delivery_price}";
	var waitprice = "{$waitprice}";
	var cartprice = "{$cart['price']}";
	var totalprice = {php echo $cart['price'] + $store['pack_price'] + $delivery_price};
	var delivery_activity_price = "{$delivery_activity_price}";
	var activity_price = "{$activity_price}";

	//配送方式
	$(document).on('click', '#order-type-1', function(){
		$('#address-container').show();
		$('#delivery-time-2').hide();
		$('#delivery-time-1').show();
		$('#delivery-price').html(delivery_price);
		$('.total-price').html('￥' + totalprice);
		$('.activity-price').html('￥' + activity_price);
		waitprice_temp = totalprice - activity_price;
		var discount = $("#discount").val();
		waitprice_temp = waitprice_temp * (parseInt(discount)/100);
		$('.wait-price').html('￥' + waitprice_temp);
		$('.activity-delivery').show();
		$('.info-bar').show();
	});

	$(document).on('click', '#order-type-2', function(){
		$('#address-container').hide();
		$('#delivery-time-2').show();
		$('#delivery-time-1').hide();
		$('#delivery-price').html(0);
		$('.activity-delivery').hide();
		$('.info-bar').hide();
		var totalprice_temp = totalprice - 0 - delivery_price;
		$('.total-price').html('￥' + totalprice_temp);
		activityprice_temp = activity_price - delivery_activity_price;
		$('.activity-price').html('￥' + activityprice_temp);
		waitprice_temp = totalprice_temp - activityprice_temp;
		waitprice_temp = waitprice_temp * (parseInt(discount)/100);
		$('.wait-price').html('￥' + waitprice_temp);
	});

	//选择时间
	$(document).on('click', '.delivery-time-show', function(){
		$.iopenModal('.delivery-time-modal', function(){
			var init_show = $('#delivery-time-children li').not('.hide').size();
			if(!init_show) {
				var now_day = $('#delivery-time-parent li.active');
				now_day.next().trigger('click');
				now_day.addClass('hide');
			}
			$('.delivery-time-modal .children-category-wrapper').height(350);
			if($.device.iphone) {
				new IScroll('.delivery-time-modal .children-category-wrapper', {probeType: 3, mouseWheel: true, click: false, tap: true})
			} else {
				new IScroll('.delivery-time-modal .children-category-wrapper', {probeType: 3, mouseWheel: true, click: true})
			}
		});
	});

	$(document).on('click', '#delivery-time-children li', function(){
		var day = $('#delivery-time-parent li.active').data('value');
		var time = $(this).data('value');
		$('#delivery-time').val(time);
		$('#delivery-day').val(day);
		$('.delivery-time-show').html(day + ' ' + time);
		$.icloseModal('.delivery-time-modal', true);
		return false;
	});
	var today_date = "{php echo date('m-d')}";
	var time_flag = "{$time_flag}";
	$(document).on('click', '#delivery-time-parent li', function(){
		$(this).siblings().removeClass('active');
		$(this).addClass('active');
		var date = $(this).data('value');
		if(today_date == date) {
			if(time_flag == 1) {
				$('#delivery-time-children li.time-flag').removeClass('hide');
			}
			$('#delivery-time-children li.init-hide').addClass('hide');
		} else {
			$('#delivery-time-children li.time-flag').addClass('hide');
			$('#delivery-time-children li.init-hide').removeClass('hide');
		}
		return false;
	});

	//备注
	$(document).on('click', '#order-note-show', function(){
		$.popup('.popup-remark');
	});

	$(document).on('click', '#popup-remark .spec-item', function(){
		var note = $('#note-textarea').val();
		$('#note-textarea').val(note + ' ' + $(this).html());
	});

	$(document).on('click', '#note-submit', function(){
		var note = $('#note-textarea').val();
		if(note == '') {
			note = '(选填)可填入特殊要求';
		}
		$('#order-note-show').html(note);
		$('#order-note').val(note);
	});

	//发票
	$(document).on('click', '.invoice-status', function(){
		console.info(($(this).find('input').prop('checked')));
		if($(this).find('input').prop('checked')) {
			$('.invoice-value').removeClass('hide');
		} else {
			$('.invoice-value').addClass('hide');
		}
	});
});
</script>
{template 'common'}
{template 'footer'}